package com.base.myproject.fileCenter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.base.myproject.server.excel.Excel2DB;

/**
 * @author cxghiu410@gmail.com
 *	这个类主要用于实现保存上传的文件，并将上传文件的信息写入hr_docCenter中。
 *	@see com.base.myproject.fileCenter.JDBCToFile
 */
public class FileManager {
	private String fileName = "";  //上传文件的文件名
	private File tmpDir = new File("d:\\temp");
	private String file = null;   //文件的生成路径名
	private String showFileName = null;  //显示的文件名
	private JDBCToFile upload = null;
	private String showImageUrl = "";  //上传文件的图标路径
	public void upload(HttpServletRequest request,String classNo, String path){
		try{
			if(ServletFileUpload.isMultipartContent(request)){
				DiskFileItemFactory factory = new DiskFileItemFactory();
				factory.setSizeThreshold(1 * 1024 * 1024);
				factory.setRepository(tmpDir);
				ServletFileUpload sfu = new ServletFileUpload(factory);
				sfu.setFileSizeMax(20 * 1024 * 1024);
				sfu.setSizeMax(20 * 1024 * 1024);
				sfu.setHeaderEncoding("UTF-8");
				FileItemIterator fii = sfu.getItemIterator(request);
				String uploadPath = path + "upload/docCenter";  //文件夹路径
				String uploadPathTomcat = "webapps/hr/upload/docCenter";  //tomcat文件路径
				upload = new JDBCToFile();
				
				if (!new File(uploadPath).isDirectory()) {
					new File(uploadPath).mkdirs();
				}
				while(fii.hasNext()){
					FileItemStream fis = fii.next();
					if(!fis .isFormField() && fis.getName().length() > 0){
						fileName = fis.getName();
						int start = fileName.lastIndexOf(".");
						if(start > 7) {
							showFileName = fileName.substring(0, 7) + "···" + fileName.substring(start);
						} else {
							showFileName = fileName;
						}
						file = fileName.substring(0, start) + System.currentTimeMillis() + fileName.substring(start);
						BufferedInputStream in = new BufferedInputStream(fis.openStream());
						BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadPath/*Tomcat*/ + "/" + file)));
						Streams.copy(in, out, true);
						in.close();
						out.close();
						String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
						if (suffix.equals("rar") || suffix.equals("xls") || suffix.equals("doc") || suffix.equals("pdf") || suffix.equals("swf") || suffix.equals("txt")|| suffix.equals("zip")) {
							showImageUrl = "images/icon/" + suffix + ".jpg";
					    } else if (suffix.equals("jpg") || suffix.equals("gif") || suffix.equals("png") || suffix.equals("jpeg")) {
					    	showImageUrl = "upload/docCenter/" + file;
					    } else {
					    	showImageUrl = "images/icon/null.jpg";
					    }
						file = uploadPath + "/" + file;
					}
					
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		upload.addImages(file,new Date(),fileName,classNo,showImageUrl,showFileName);
	}
	
	public int uploadExcel(HttpServletRequest request, String path){
		int success = 0;
		try{
			if(ServletFileUpload.isMultipartContent(request)){
				DiskFileItemFactory factory = new DiskFileItemFactory();
				factory.setSizeThreshold(1 * 1024 * 1024);
				factory.setRepository(tmpDir);
				ServletFileUpload sfu = new ServletFileUpload(factory);
				sfu.setFileSizeMax(20 * 1024 * 1024);
				sfu.setSizeMax(20 * 1024 * 1024);
				sfu.setHeaderEncoding("UTF-8");
				FileItemIterator fii = sfu.getItemIterator(request);
				String uploadPath = path + "upload/excel";  //文件夹路径
				//String uploadPathTomcat = "webapps/hr/upload/docCenter";  //tomcat文件路径
				
				if (!new File(uploadPath).isDirectory()) {
					new File(uploadPath).mkdirs();
				}
				
				while(fii.hasNext()){
					FileItemStream fis = fii.next();
					if(!fis .isFormField() && fis.getName().length() > 0){
						fileName = fis.getName();
						File file = new File(uploadPath/*Tomcat*/ + "/" + fileName);
						BufferedInputStream in = new BufferedInputStream(fis.openStream());
						BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
						Streams.copy(in, out, true);
						in.close();
						out.close();
						
						Excel2DB excel = new Excel2DB(uploadPath/*Tomcat*/ + "/" + fileName);
						success = excel.Import();
						
						file.delete();
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}
	
	public void deleteFile(String name) {
		File file = new File(name);
		file.delete();
	}
}
